#include "stdafx.h"
#include "AppClient.h"
#include "AppConfigClient.h"
#include <mmsystem.h>

CAppClient::CAppClient()
{
	m_bQuit = false;
}

CAppClient::~CAppClient()
{

}

bool CAppClient::Run(HINSTANCE hInstance)
{
	Init(hInstance);
	while (MainFrame());
	return true;
}

void CAppClient::Quit()
{
	m_bQuit = true;
}

UINT32 CAppClient::OnInputMessage(UINT32 uMsg, UINT32 wParam, UINT32 lParam)
{
	event _event;
	_event.bProcess = false;
	_event.Messtype = uMsg;
	_event.wParam = wParam;
	_event.lParam = lParam;
	_event.mPoint.x = LOWORD(lParam);
	_event.mPoint.y = HIWORD(lParam);
	return 0;
}

void CAppClient::Init(HINSTANCE hInstance)
{
	new CAppConfigClient( TEXT("../Resource/xml/configure.xml"));
	UINT32 width = CAppConfigClient::getSingletonPtr()->GetWindowWidth();
	UINT32 height = CAppConfigClient::getSingletonPtr()->GetWindowHeight();
	m_pMainWnd = new CMainWnd( this, hInstance, width, height, TEXT(""), TEXT(""), TEXT("") );

}

void CAppClient::Init(HWND hWnd)
{

}


bool CAppClient::MainFrame()
{
	CRenderSystem* pRenderSystemp = CRenderSystem::getSingletonPtr();
	UINT32 nowtimer = timeGetTime();
	UINT32 nextLoop = 0;
	UINT32 FrameCyc = CAppConfigClient::getSingletonPtr()->GetFrameCycTimer();
	while (!m_bQuit)
	{
		nowtimer = timeGetTime();
		OnSysMsg();
		if (nowtimer >= nextLoop)
		{
			nextLoop = nowtimer + FrameCyc;
			Frame(pRenderSystemp, nowtimer);

			nextLoop = nowtimer + FrameCyc;
		}
		else
		{
			Sleep(nextLoop - nowtimer);
		}
	}
	return !m_bQuit;
}

void CAppClient::OnSysMsg()
{
	MSG msg;
	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessageW(&msg);
	}

}

void CAppClient::Frame(CRenderSystem* pRenderSystemp, UINT32 nowtimer)
{

}

